package com.zqh.myfmmall.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zqh.myfmmall.doc.ProductDocument;
import com.zqh.myfmmall.entity.*;
import com.zqh.myfmmall.result.Result;
import com.zqh.myfmmall.service.*;
import com.zqh.myfmmall.vo.ProductVo;
import io.swagger.annotations.Api;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表(Product)表控制层
 *
 * @author KONO 曾淇杭哒！
 * @since 2022-01-20 20:53:00
 */
@RestController
@RequestMapping("product")
@Api(tags = "商品管理")
public class ProductController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;
    @Resource
    private ProductImgService productImgService;
    @Resource
    private ProductCommentsService productCommentsService;
    @Resource
    private ProductParamsService productParamsService;
    @Resource
    private ProductSkuService productSkuService;


    /**
     * 功能描述:
     * 暂时只简单实现，通过文本进行match模糊搜索和分页，更多的搜索条件后期需要再添加
     * @param key pageNum pageSize
     * @return
     * @author kono曾淇杭哒！
     * @date 2022/2/13 23:51
     */
    @GetMapping("search/{key}/{pageNum}/{pageSize}")
    public Result search(@PathVariable("key") String key,
                         @PathVariable("pageNum") Integer pageNum,
                         @PathVariable("pageSize") Integer pageSize) throws Exception{
        return productService.search(key, pageNum, pageSize);
    }

    /**
     * 功能描述:
     * 获取最新上架商品
     *
     * @param
     * @return
     * @author kono曾淇杭哒！
     * @date 2022/1/25 2:11
     */
    @GetMapping("latest")
    public Result getLatest() throws JsonProcessingException {
        Result result = productService.latestProducts();
        return result;
    }


    /**
     * 功能描述:
     * 获取商品详细信息（基本信息，图片，详细参数，套餐sku，评价信息）
     *
     * @param pid
     * @return com.zqh.myfmmall.result.Result
     * @author kono曾淇杭哒！
     * @date 2022/1/25 23:44
     */
    @GetMapping("detail/{pid}")
    public Result producDetail(@PathVariable("pid") String pid){
        Product product = productService.getById(pid);
        List<ProductImg> productImgs = productImgService.selectProductImgByProductId(pid);
        ProductParams productParams = productParamsService.selectByProductId(pid);
        Map<String, Integer> commentsCountMap = productCommentsService.getProductCommentsCountsByProductId(pid);
        List<ProductSku> productSkus = productSkuService.selectByProductId(pid);
        ProductVo productVo = new ProductVo(product);
        productVo.setProductImgs(productImgs);
        productVo.setProductParams(productParams);
        productVo.setProductSkus(productSkus);
        productVo.setCommentsCount(commentsCountMap);
        return Result.success(productVo);
    }


}

